<?php
class DangkiesController extends AppController {

	var $name = 'Dangkies';
    var $components = array('Common','Session','Auth','Email', 'RequestHandler','Redirect');
    var $helpers = array('Html', 'Js','Form','Ajax','Javascript');
    var $uses = array('Dangky','Loaihoso','Yeucaucm','Yeucautd','Trinhdo','Chuyenmon','Noilamviec','Tinhtp','User');
    
    function beforeFilter(){ 
        parent::beforeFilter(); 
        $this->Auth->allow('index'); // hàm cho phép truy cập function view 
    }
    
	function index() 
    {
		$this->Dangky->recursive = 2;
		$this->set('dangkies', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid dangky', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('dangky', $this->Dangky->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Dangky->create();
			if ($this->Dangky->save($this->data)) {
				$this->Session->setFlash(__('The dangky has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dangky could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Dangky->User->find('list');
		$loaihosos = $this->Dangky->Loaihoso->find('list');
		$linhvucs = $this->Dangky->Linhvuc->find('list');
		$this->set(compact('users', 'loaihosos', 'linhvucs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid dangky', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Dangky->save($this->data)) {
				$this->Session->setFlash(__('The dangky has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dangky could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Dangky->read(null, $id);
		}
		$users = $this->Dangky->User->find('list');
		$loaihosos = $this->Dangky->Loaihoso->find('list');
		$linhvucs = $this->Dangky->Linhvuc->find('list');
		$this->set(compact('users', 'loaihosos', 'linhvucs'));
	}

	function delete($id = null)
   {
       if (!$id)
       {
           $this->Redirect->flashWarning('Bài viết không tồn tại', array('action'=>'index'));
       }
       $dk=$this->Dangky->find('first',array('conditions'=>array('Dangky.id'=>$id)));//tìm user_id của post
       if ($dk['Dangky']['users_id']==$this->Auth->user('id'))
       {
           if($this->Dangky->delete($id))
           {
              $this->Redirect->flashSuccess('Xóa bài viết thành công',array('action'=>'index'));
           }
           else $this->Redirect->flashWarning('Xóa thông tin bài viết thất bại', array('action'=>'index'));
       }
       else $this->Redirect->flashWarning('Bạn không có quyền xóa bài viết này', array('action'=>'index'));
   }

   function dangtin(){
        
    if(!empty($this->data))
    {
        $record['Dangky']['soluong']  =$this->data['Dangky']['sl'];
        $record['Dangky']['kinhnghiem']  =$this->data['Dangky']['kn'];
        $record['Dangky']['hinhthuc']=$this->data['Dangky']['ht'];
        $record['Dangky']['user_id']  =1;
        $record['Dangky']['tencv']   =$this->data['Dangky']['tcv'];
        $record['Dangky']['mucluong']=$this->data['Dangky']['ml'];
        $record['Dangky']['ghi_chu']  =$this->data['Dangky']['gc'];
        $record['Dangky']['thoihan']  =$this->data['Dangky']['th'];
        $record['Dangky']['ngaydang'] =date('Y-m-d');
        $record['Dangky']['users_id'] =$this->data['Dangky']['users_id'];
        if(!$this->Dangky->saveAll($record))
            $this->Session->setflash('sai');
        //lay bien dk
        $dk_id = $this->Dangky->id;
        //debug($this->data);
        //noi lam viec
        $this->Noilamviec->create();
        foreach($this->data['Dangky']['tinhtp'] as $value)
        {
            $record['Noilamviec']['dangkies_id']=$dk_id;
            $record['Noilamviec']['id_t']=$value;
            if(!$this->Noilamviec->saveAll($record))
                $this->Session->setflash('sai');
        }
        //chuyen mon
        $this->Yeucaucm->create();
        foreach($this->data['Dangky']['chuyenmon'] as $value)
        {
            $record['Yeucaucm']['chuyenmon_id']=$value;
            $record['Yeucaucm']['dangkies_id']=$dk_id;
            if(!$this->Yeucaucm->saveAll($record))
            $this->Session->setflash('sai');
        }
                 //trinh do
        $this->Yeucautd->create();
        foreach($this->data['Dangky']['trinhdo'] as $value)
        {
            $record['Yeucautd']['trinhdos_id']=$value;
            $record['Yeucautd']['dangkies_id']=$dk_id;
            if(!$this->Yeucautd->saveAll($record))
            $this->Session->setflash('sai');
        }
        $this->Redirect->flashSuccess('Đăng thông tin tuyển dụng thành công', array('action' =>'index'));
    }
     $trinhdos = $this->Trinhdo->find('list',array('fields'=>array('diengiaitd')));
     $chuyenmons = $this->Chuyenmon->find('list',array('fields'=>array('diengiaicm')));
     $tinhtps = $this->Tinhtp->find('list',array('fields'=>array('tenttp')));
     $this->set(compact('trinhdos','chuyenmons','tinhtps'));
     
    }
    
    
    function tintuyendung()
    {
        $dangkies= $this->Dangky->find('all', array('conditions' => array('Dangky.users_id'=>$this->Auth->user('id')),'Dangky.ngaydang DESC'));
        $this->set('dangkies',$dangkies);
    }

    function hosoungvien()
    {
        $dangkies= $this->Dangky->find('all', array('conditions' => array($group_id=>2)));
        $this->set('dangkies',$dangkies);
    }

    function search() 
    {
        
     $chuyenmons = $this->Chuyenmon->find('list',array('fields'=>array('diengiaicm')));
     $tinhtps = $this->Tinhtp->find('list',array('fields'=>array('tenttp')));
      
    $this->Dangky->bindModel
    (
     array(
          'hasOne' => array(
                    'Noilamviec',
                    'FilterTinhtp' => array
                        (
                            'className' => 'Tinhtp',
                            'foreignKey' => false,
                            'conditions' => array('FilterTinhtp.id = Noilamviec.id_t')
                        )
                      
                    )
                    
                    
                    
         )
    );
    
     $this->Dangky->bindModel
    (
     array(
          'hasOne' => array(
                    'Yeucaucm',
                    'FilterChuyenmon' => array
                        (
                            'className' => 'Chuyenmon',
                            'foreignKey' => false,
                            'conditions' => array('FilterChuyenmon.id = Yeucaucm.chuyenmon_id')
                        )
                      
                    )
                    
                    
                    
         )
    );
    $tin=$this->Dangky->find('all', array(
    'fields' => array('Dangky.*'),
    'conditions'=>array('FilterTinhtp.id'=> ($this->data['Dangky']['tinhtp_id']),'FilterChuyenmon.id'=> ($this->data['Dangky']['chuyenmon_id'])
    )));


    $this->set(compact('chuyenmons','tinhtps','tin'));;
        
           
    }
    
    function dsungvien($id=null)
    {
    	if(!$id)
    		$this->Redirect->flashSuccess('Mẫu tin không hợp lệ',array('action'=>'tintuyendung'));
    	
    	$tindang = $this->Dangky->find('first',array('recursive'=>1,'conditions'=>array('Dangky.id'=>$id)));
    	
    	$tinhs = array();
    	$chuyenmons = array();
    	$trinhdos = array();
    	
    	$group_id = $tindang['Users']['group_id'];
    	foreach($tindang['Tinhtp'] as $tinh)
    	{
    		$tinhs[$tinh['id']]=$tinh['id'];
    	}
    	foreach($tindang['Chuyenmon'] as $cm)
    	{
    		$chuyenmons[$cm['id']]=$cm['id'];
    	}
    	foreach($tindang['Trinhdo'] as $td)
    	{
    		$trinhdos[$td['id']]=$td['id'];
    	}
    	$joins =array(array('table' => 'Noilamviecs'
	            						,'type' => 'INNER'
	            						,'alias' => 'Noilamviec'
	            						,'conditions' => array(
	                					'Dangky.id = Noilamviec.dangky_id')),
	            			array('table' => 'Yeucaucms'
	            						,'type' => 'INNER'
	            						,'alias' => 'Yeucaucm'
	            						,'conditions' => array(
	                					'Dangky.id = Yeucaucm.dangky_id')),
	            			array('table' => 'Yeucautds'
	            						,'type' => 'INNER'
	            						,'alias' => 'Yeucautd'
	            						,'conditions' => array(
	                					'Dangky.id = Yeucautd.dangky_id')));
    	$this->paginate = array('joins'=>$joins,'conditions'=>array('Users.group_id'=>($group_id==1?2:1),'trinhdo_id'=>$trinhdos,'chuyenmon_id'=>$chuyenmons,'id_t'=>$tinhs),'order'=>'ngaydang DESC');
    	$ungviens = $this->paginate();
    	
    	$this->set(compact('ungviens'));    	
    }

    function exampleautocomplete()
    {
    	
    }
    
	function autocomplete()
	{
		$this->layout='ajax';
		
		$value = strtolower($this->params['url']['term']);
		$array = array('a','b','c');
		$result = array();
		foreach ($array as $key=>$value)
			array_push($result, array("id"=>$key, "value" => $value));
		
	$result=array_to_json($result);
	$this->set(compact('result'));
	}
}
	function array_to_json( $array )
	{

	    if( !is_array( $array ) ){
	        return false;
	    }
	
	    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
	    if( $associative ){
	
	        $construct = array();
	        foreach( $array as $key => $value ){
	
	            // We first copy each key/value pair into a staging array,
	            // formatting each key and value properly as we go.
	
	            // Format the key:
	            if( is_numeric($key) ){
	                $key = "key_$key";
	            }
	            $key = "\"".addslashes($key)."\"";
	
	            // Format the value:
	            if( is_array( $value )){
	                $value = array_to_json( $value );
	            } else if( !is_numeric( $value ) || is_string( $value ) ){
	                $value = "\"".addslashes($value)."\"";
	            }
	
	            // Add to staging array:
	            $construct[] = "$key: $value";
	        }
	
	        // Then we collapse the staging array into the JSON form:
	        $result = "{ " . implode( ", ", $construct ) . " }";
	
	    } else { // If the array is a vector (not associative):
	
	        $construct = array();
	        foreach( $array as $value ){
	
	            // Format the value:
	            if( is_array( $value )){
	                $value = array_to_json( $value );
	            } else if( !is_numeric( $value ) || is_string( $value ) ){
	                $value = "'".addslashes($value)."'";
	            }
	
	            // Add to staging array:
	            $construct[] = $value;
	        }
	
	        // Then we collapse the staging array into the JSON form:
	        $result = "[ " . implode( ", ", $construct ) . " ]";
	    }
	
	    return $result;
	}
?>